package com.cike.productmanager.DBHelper;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.logan.productchannelmanager.entity.StoreAndActivityRecord;
import com.logan.productchannelmanager.entity.StoreAndActivityRecord.ActivitySimpleMsg;
import com.logan.productchannelmanager.entity.StoreAndActivityRecord.StoreSimpleMsg;
import com.logan.productchannelmanager.util.DataApplication;
import com.logan.productchannelmanager.util.HttpUtil;
import com.logan.productchannelmanager.util.IConst;

/**************************************************************************************
 * �ξ�����ӡ�2012.12.26
 **************************************************************************************/
@SuppressLint("NewApi")
public class DBManager {
	private static final String DATABASE_NAME = "test.db";
	private static final int DATABASE_VERSION = 1;
	private static SQLiteDatabase db = null;
	private static DBHelper DBhelper=null;
	private static List<StoreInfo> storelist=new ArrayList<StoreInfo>();
	private static List<ActivityInfo> activitylist=new ArrayList<ActivityInfo>();
	private static List<ExguideInfo> exguidelist=new ArrayList<ExguideInfo>();
	private static List<String> storeerealist=new ArrayList<String>();//��
	private static List<String> storetypelist=new ArrayList<String>();//�ŵ�����
	private static List<String> storecustomerlist=new ArrayList<String>();//�ŵ�ͻ�
	
	public DBManager() {
		super();
	}
	
	/**
	 * ����ݿ�
	 */
	public static Boolean openDB(Context context) {
		
			DBhelper  = new DBHelper(context, DATABASE_NAME, null,
					DATABASE_VERSION);
			db = DBhelper.getReadableDatabase();
			if (db != null) {		// TODO ������ݿ�󣬶���ݿ�Ĳ���  
				
				return true;
			}
		return false;
	}

	public static void CreateTable()
	{
		//1.�ŵ��
		db.execSQL("CREATE TABLE IF NOT EXISTS store_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+" storecode VARCHAR,"//�ŵ����
				+ " storename VARCHAR,"//�ŵ����
				+" type VARCHAR,"//�ŵ�����
				+ " khzb VARCHAR,"//�ͻ��ܲ�" customerhq VARCHAR,"
				+" customer VARCHAR,"//�ͻ����+ " customername VARCHAR,"
				+ " province VARCHAR,"//ʡ
				+ " city VARCHAR,"//��
				+ " erea VARCHAR,"//��
				+ " street VARCHAR,"//��
				+ " phone VARCHAR,"//�绰����
				+ " lw VARCHAR,"//��γ��
				+ " customermg VARCHAR,"//�ͻ�����
				+ " tallyman VARCHAR,"//���Ա
				+" storegroupid VARCHAR)"); //�ŵ���
		
		//2.���
		db.execSQL("CREATE TABLE IF NOT EXISTS activity_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+" name VARCHAR,"//����
				+" id VARCHAR,"//�id
				+" type VARCHAR,"//�����
				+" storename VARCHAR,"//�ŵ����
				+" storecode VARCHAR,"//�ŵ���
				+" storetype VARCHAR,"//�ŵ�����
				+" display VARCHAR,"//�ƻ����з�ʽ
				+" displaycount VARCHAR,"//��������
				+" starttime VARCHAR,"//���п�ʼʱ��
				+" endtime VARCHAR,"//���н���ʱ��
				+" promostime VARCHAR,"//����Ա��ʱ��
				+" promoetime VARCHAR,"//����Ա�˳�ʱ��
				+" promocount VARCHAR,"//����Ա����
				+" promostate VARCHAR,"//����Ա״̬
				+" promoproductstate VARCHAR,"//�����Ʒ�Ƿ񵽴�
				+" materialstate VARCHAR,"//���ϵ���״̬
				+" realdisplay VARCHAR,"//ʵ�ʳ��з�ʽ
				+" isphoto VARCHAR,"//�Ƿ�����
				+" level VARCHAR,"//ִ�н��
				+" content VARCHAR,"//�����
				+" state VARCHAR,"//�״̬
				+" imagepath VARCHAR,"//ͼƬ·��
				+" isNew VARCHAR,"//�Ƿ����»
				+" remark VARCHAR)"); //��ע
		
		/*add by ADao12*///TODO create the acitvitycheck_table
		db.execSQL("CREATE TABLE IF NOT EXISTS activitycheck_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+" check_id VARCHAR,"         //�������Id
				+" activity_id VARCHAR," //�id
				+" store_code VARCHAR,"  //�ŵ����
				+" promostate VARCHAR,"  // ����Ա״̬
				+" promoproductstate VARCHAR,"// �����Ʒ�Ƿ񵽴�
				+" materialstate VARCHAR,"// ���ϵ���״̬
				+" realdisplay VARCHAR,"  // ʵ�ʳ��з�ʽ
				+" isphoto VARCHAR,"     // �Ƿ�����
				+" level VARCHAR,"        // ����
				+" state VARCHAR,"        //�״̬
				+" remark VARCHAR,"       //��ע
				+" imagePath VARCHAR,"   //ͼƬ·��
				+" isOld)");     //�Ƿ�Ϊ��ͬ����Ϣ
		 
		//3.��ͬ��
		db.execSQL("CREATE TABLE IF NOT EXISTS contract_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
				+" name VARCHAR,"//��ͬ���
				+" id VARCHAR,"//��ͬid
				+" storename VARCHAR,"//�ŵ����
				+" storecode VARCHAR,"//�ŵ���
				+" type VARCHAR,"//��ͬ����
				+" starttime VARCHAR,"//��ͬ��ʼʱ��
				+" endtime VARCHAR,"//��ͬ����ʱ��
				+" isphoto VARCHAR,"//�Ƿ�����
				+" content VARCHAR,"//��ͬ����
				+" state VARCHAR,"//��ִͬ��״̬
				+" imagepath VARCHAR,"//ͼƬ·��
				+" isNew VARCHAR,"//�Ƿ����º�ͬ
				+" remark VARCHAR)"); //��ע
		
		// added by xieyan  2013 01 30 
		db.execSQL("CREATE TABLE IF NOT EXISTS exguide_info(_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                +" id VARCHAR,"//ָ��id
                +" No VARCHAR,"//ָ����
                +" name VARCHAR,"//ָ�����
                +" imgpath VARCHAR,"//ͼƬ·��
                +" remark VARCHAR,"//��ע
                +" type VARCHAR"//ָ������
                +" )"); 
	}
	
	/**
	 * �ر���ݿ�
	 */
	public static void closeDB() {
		if (db != null) {
			db.close();
			
		}
		DBhelper=null;
	}

	/*
	 * ɾ��������ݱ�
	 */
	public static void deleteAllTable()
	{
		
		db.execSQL("DROP TABLE IF EXISTS store_info"); 
		db.execSQL("DROP TABLE IF EXISTS activity_info");
		db.execSQL("DROP TABLE IF EXISTS activitycheck_info");
		db.execSQL("DROP TABLE IF EXISTS exguide_info");
	}
	
	
	
	/****************************************************************************************************
	 *                                        �ŵ����ݿ����
	 ***************************************************************************************************/
	
	/**
     * ���������ŵ����Ϣ
     * @return
     */
    public static List<StoreInfo> loadStoreInfoFromDB(Context context)
    {
    	if(storelist.size()>0)
    	{
    		return storelist;
    	}
    	else
    	{
    		int total=DBManager.getStoreInfoCount(context);
    	   
        	for(int uid=1;uid<total;uid++)
        	{
        		StoreInfo store=DBManager.findStoreInfo(context, uid);
				storelist.add(store);
				
				String erea=store.getErea();
				String type=store.getStoreType();
				String customer=store.getCustomName();
				if (customer==null) {
					customer="NULL";
				}
				//������
				Boolean found=false;
				for(String temp:storeerealist)
				{
					if(temp.matches(erea))
					{
						found=true;
					}
				}
				if(!found)
				{
					storeerealist.add(erea);
				}
				//����ͻ�
				 found=false;
				for(String temp:storecustomerlist)
				{
					if(temp.matches(customer))
					{
						found=true;
					}
				}
				if(!found)
				{
					storecustomerlist.add(customer);
				}
				//�����ŵ�����
				 found=false;
				for(String temp:storetypelist)
				{
					if(temp.matches(type))
					{
						found=true;
					}
				}
				if(!found)
				{
					storetypelist.add(type);
				}
        	}
        	return storelist;
    	}

    }
    /**
     * ��ȡ�ŵ����е���
     */
    public static List<String> getStoreEreas()
    {
    	return storeerealist;
    }
    /**
     * ��ȡ�ŵ����е�����
     */
    public static List<String> getStoreTypes()
    {
    	return storetypelist;
    }
    /**
     * ��ȡ�ŵ����еĿͻ�
     */
    public static List<String> getStoreCustomers()
    {
    	return storecustomerlist;
    }

    
	/**
	 * ��ȡ�ŵ���Ϣ�ܼ�¼��
	 * @param context
	 * @return
	 */
	public static int getStoreInfoCount(Context context)
	{
		if(db==null)
		{
			openDB(context);
		}
		//Cursor���α��� �α�����ݿ�����ʵ����һ����ݼ�
        Cursor cursor = db.rawQuery("select count(*) from store_info", null);
		if(cursor.moveToLast()){
		return cursor.getInt(0);
		}
		return 0;
		
	}
	/**
	 * ����ŵ��ţ���ȡ�ŵ����Ϣ�б�
	 */
	public static List<StoreInfo> findStoreInfo(Context context, String sarea ,String stype,String scustomer)
	{

		try {
			if (db == null) {
				openDB(context);
			}
			String sql="";
			String[] selectArgs=null;
			if(sarea.length()==0)sarea=null;
			if(stype.length()==0)stype=null;
			if(scustomer.length()==0)scustomer=null;
			
			if(sarea==null&&stype==null&&scustomer==null)
			{
				return loadStoreInfoFromDB(context);//����ȫ���ŵ���Ϣ
			}
			else if(sarea!=null&&stype!=null&&scustomer!=null)
			{
				sql="select * from store_info where erea=? and type=? and customer=?";
				selectArgs=new String[] { sarea,stype,scustomer};
			}
			else if(sarea!=null&&stype==null&&scustomer==null)
			{
				sql="select * from store_info where erea=?";
				selectArgs=new String[] { sarea};
			}
			else if(sarea==null&&stype!=null&&scustomer==null)
			{
				sql="select * from store_info where type=?";
				selectArgs=new String[] {stype};
			}
			else if(sarea==null&&stype==null&&scustomer!=null)
			{
				sql="select * from store_info where customer=?";
				selectArgs=new String[] {scustomer};
			}
			else if(sarea==null&&stype!=null&&scustomer!=null)
			{
				sql="select * from store_info where type=? and customer=?";
				selectArgs=new String[] {stype,scustomer};
			}
			else if(sarea!=null&&stype==null&&scustomer!=null)
			{
				sql="select * from store_info where erea=? and customer=?";
				selectArgs=new String[] { sarea,scustomer};
			}
			else if(sarea!=null&&stype!=null&&scustomer==null)
			{
				sql="select * from store_info where erea=? and type=?";
				selectArgs=new String[] { sarea,stype};
			}
				
			Cursor cursor = db.rawQuery(sql,selectArgs);

			List<StoreInfo> findStorelist=new ArrayList<StoreInfo>();
			try{
			while (cursor.moveToNext()) {
				
				String storecode = cursor.getString(cursor.getColumnIndex("storecode"));
				String stroename = cursor.getString(cursor
						.getColumnIndex("storename"));
				
				String type = cursor.getString(cursor.getColumnIndex("type"));

				String khzb = cursor.getString(cursor.getColumnIndex("khzb"));
				String customer = cursor.getString(cursor
						.getColumnIndex("customer"));
				String province = cursor.getString(cursor
						.getColumnIndex("province"));
				String city = cursor.getString(cursor.getColumnIndex("city"));
				String erea = cursor.getString(cursor.getColumnIndex("erea"));
				String street = cursor.getString(cursor
						.getColumnIndex("street"));
				String phone = cursor.getString(cursor.getColumnIndex("phone"));
				String lw = cursor.getString(cursor.getColumnIndex("lw"));
				String customermg = cursor.getString(cursor
						.getColumnIndex("customermg"));
				String tallyman = cursor.getString(cursor
						.getColumnIndex("tallyman"));
				
				String storegroupid = cursor.getString(cursor
						.getColumnIndex("storegroupid"));

				StoreInfo storeInfo = new StoreInfo();
				storeInfo.setStoreCode(storecode);
				storeInfo.setStoreName(stroename);
				storeInfo.setStoreType(type);
				storeInfo.setCustomhq(khzb);
				storeInfo.setCustomName(customer);
				storeInfo.setProvince(province);
				storeInfo.setCity(city);
				storeInfo.setErea(erea);
				storeInfo.setStreet(street);
				storeInfo.setPhone(phone);
				storeInfo.setLW(lw);
				storeInfo.setCustomerMG(customermg);
				storeInfo.setTallyman(tallyman);
				storeInfo.setGroudId(storegroupid);
                
				findStorelist.add(storeInfo);
				
				
			}
			}catch (Exception e) {
				e.printStackTrace();
			}

			
			cursor.close();
			return findStorelist;
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * ����ŵ��ţ���ȡ�ŵ����Ϣ
	 */
	public static StoreInfo findStoreInfo(Context context, String stroecode)
	{

		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from store_info where storecode=?",
					new String[] { stroecode});

			if (cursor.moveToFirst()) {
				
				String storecode = cursor.getString(cursor.getColumnIndex("storecode"));
				String stroename = cursor.getString(cursor
						.getColumnIndex("storename"));
				
				String type = cursor.getString(cursor.getColumnIndex("type"));

				String khzb = cursor.getString(cursor.getColumnIndex("khzb"));
				String customer = cursor.getString(cursor
						.getColumnIndex("customer"));
				String province = cursor.getString(cursor
						.getColumnIndex("province"));
				String city = cursor.getString(cursor.getColumnIndex("city"));
				String erea = cursor.getString(cursor.getColumnIndex("erea"));
				String street = cursor.getString(cursor
						.getColumnIndex("street"));
				String phone = cursor.getString(cursor.getColumnIndex("phone"));
				String lw = cursor.getString(cursor.getColumnIndex("lw"));
				String customermg = cursor.getString(cursor
						.getColumnIndex("customermg"));
				String tallyman = cursor.getString(cursor
						.getColumnIndex("tallyman"));
				
				String storegroupid = cursor.getString(cursor
						.getColumnIndex("storegroupid"));

				StoreInfo storeInfo = new StoreInfo();
				storeInfo.setStoreCode(storecode);
				storeInfo.setStoreName(stroename);
				storeInfo.setStoreType(type);
				storeInfo.setCustomhq(khzb);
				storeInfo.setCustomName(customer);
				storeInfo.setProvince(province);
				storeInfo.setCity(city);
				storeInfo.setErea(erea);
				storeInfo.setStreet(street);
				storeInfo.setPhone(phone);
				storeInfo.setLW(lw);
				storeInfo.setCustomerMG(customermg);
				storeInfo.setTallyman(tallyman);
				storeInfo.setGroudId(storegroupid);
                
				cursor.close();
				return storeInfo;
			}

			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	
	/**
	 * * �����ŵ����Ϣ��һ�����
	 */
	public static StoreInfo findStoreInfo(Context context, Integer uid) {

		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from store_info where _id=?",
					new String[] { uid.toString() });

			if (cursor.moveToFirst()) {
				
				String storecode = cursor.getString(cursor.getColumnIndex("storecode"));
				String stroename = cursor.getString(cursor
						.getColumnIndex("storename"));
				
				String type = cursor.getString(cursor.getColumnIndex("type"));

				String khzb = cursor.getString(cursor.getColumnIndex("khzb"));
				String customer = cursor.getString(cursor
						.getColumnIndex("customer"));
				String province = cursor.getString(cursor
						.getColumnIndex("province"));
				String city = cursor.getString(cursor.getColumnIndex("city"));
				String erea = cursor.getString(cursor.getColumnIndex("erea"));
				String street = cursor.getString(cursor
						.getColumnIndex("street"));
				String phone = cursor.getString(cursor.getColumnIndex("phone"));
				String lw = cursor.getString(cursor.getColumnIndex("lw"));
				String customermg = cursor.getString(cursor
						.getColumnIndex("customermg"));
				String tallyman = cursor.getString(cursor
						.getColumnIndex("tallyman"));
				
				String storegroupid = cursor.getString(cursor
						.getColumnIndex("storegroupid"));

				if(storegroupid==null)
				{
					storegroupid="A";
				}
				StoreInfo storeInfo = new StoreInfo();
				storeInfo.setStoreCode(storecode);
				storeInfo.setStoreName(stroename);
				storeInfo.setStoreType(type);
				storeInfo.setCustomhq(khzb);
				storeInfo.setCustomName(customer);
				storeInfo.setProvince(province);
				storeInfo.setCity(city);
				storeInfo.setErea(erea);
				storeInfo.setStreet(street);
				storeInfo.setPhone(phone);
				storeInfo.setLW(lw);
				storeInfo.setCustomerMG(customermg);
				storeInfo.setTallyman(tallyman);
				storeInfo.setGroudId(storegroupid);
                
				cursor.close();
				return storeInfo;
			}

			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}

	
	/**
	 * �����ŵ���Ϣ��ȫ������
	 * 
	 * @throws Exception
	 */
	public static void loadStoreInfo(Context context, String strURL)
			throws Exception {
		try{

		// �ŵ����Ϣ
		String response = HttpUtil.HTTPGet(strURL);
		List<StoreInfo> storeInfos = StoreInfoSAXService.readXml(response);
		if (storeInfos.size() > 0) {

			InsertStoreDB(storeInfos);
		}
		}catch (Exception e) {
			Toast.makeText(context, "�����ŵ���Ϣ����", Toast.LENGTH_SHORT).show();
			Log.e("db-info", "Exception : "+e.getMessage());
		}

	}

	/**
	 * �ŵ����Ϣ��ݲ�����ݿ�
	 */
	public static void InsertStoreDB(List<StoreInfo> infos) {

		// ContentValues�Լ�ֵ�Ե���ʽ������
       try {

		for (StoreInfo storeInfo : infos) {

			ContentValues cv = new ContentValues();

			cv.put("storecode", storeInfo.getStoreCode());
			cv.put("storename", storeInfo.getStoreName());
			cv.put("type", storeInfo.getStoreType());
			cv.put("khzb", storeInfo.getCustomhq());
			cv.put("customer", storeInfo.getCustomName());
			cv.put("province", storeInfo.getProvince());
			cv.put("city", storeInfo.getCity());
			cv.put("erea", storeInfo.getErea());
			cv.put("street", storeInfo.getStreet());

			cv.put("phone", storeInfo.getPhone());
			cv.put("lw", storeInfo.getLW());
			cv.put("customermg", storeInfo.getCustomerMG());
			cv.put("tallyman", storeInfo.getTallyman());
			 String groupid=storeInfo.getGroupId();
			 if(groupid==null)
			 {
				 groupid="A";
			 }
			cv.put("storegroupid",groupid);
			
			db.insert("store_info", null, cv);
		}
		
   	} catch (Exception e) {
   	 Log.e("db-info", "Exception : "+e.getMessage());
   	}
	}

	/**
	 * ����ݿ��л�ȡ�ŵ�����  
	 * @param context
	 * @return
	 */
	public static String queryStoreGroudId(Context context) {//TODO ??

		String result = null;
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.query("store_info", new String[] { "_id",
					"groupid" }, "_id=?", new String[] { "1" }, null, null,
					null);
			cursor.moveToNext();
			result = cursor.getString(cursor.getColumnIndex("groupid"));
			Log.i("ChannelManager", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}
	
	/********************************************************************************
	 *                                  ���ݴ���
	 ********************************************************************************/
	/**
	 * �ѻ��ݲ�����ݿ�
	 */
	public static void InsertActivityDB(List<ActivityInfo> list)
	{
		for (ActivityInfo activityInfo : list) {

			ContentValues cv = new ContentValues();

			cv.put("id", activityInfo.id);
			cv.put("storecode", activityInfo.code);
			cv.put("name", activityInfo.name);
			cv.put("type", activityInfo.type);
			cv.put("starttime", activityInfo.startTime);
			cv.put("endtime", activityInfo.endTime);
			cv.put("level", activityInfo.level);
			cv.put("content", activityInfo.content);
			cv.put("state", activityInfo.state);
			cv.put("imagepath", activityInfo.imagePath);
			cv.put("remark", activityInfo.remark);
            //��������
			cv.put("storename", activityInfo.storeName);//�ŵ����
			cv.put("storetype", activityInfo.storeType);//�ŵ�����
			cv.put("display", activityInfo.display);
			cv.put("displaycount", activityInfo.displaycount);
			cv.put("promostime", activityInfo.promostime);
			cv.put("promoetime", activityInfo.promoetime);
			cv.put("promocount", activityInfo.promocount);
			cv.put("promostate", activityInfo.promostate);
			cv.put("realdisplay", activityInfo.realdisplay);
			cv.put("isphoto", activityInfo.isphoto);
			cv.put("isNew", "Yes");
			db.insert("activity_info", null, cv);
			
			/*�������ص���ݾ���Ϊ�£���Yes������������������������ʱע��*/
			/*����ݴ������activitycheck_info��*/
			try{
				if (activityInfo != null) {
					ActivityCheckInfo activityCheckInfo = transActivityInfoIntoActivityCheckInfo(activityInfo);	
					if (activityCheckInfo != null) {//�����Ϊ�ɻ
						InsertActivityCheckDB(activityCheckInfo);
					}
				}
			
			}catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	/**
	 * ������������л���
	 * @param context
	 */
	public static void loadActivityInfo(Context context,String strUrl)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			String response=HttpUtil.HTTPGet(strUrl);//������������л���
			Log.i("Info","���л��"+response);
			 List<ActivityInfo> activityInfos = ActivityInfoSAXService.readXml(response);//����
			InsertActivityDB(activityInfos);//�ѻ��ݲ�����ݿ�
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * ��ȡ���Ϣ�ܼ�¼��
	 * @param context
	 * @return
	 */
	public static int getActivityInfoCount(Context context)
	{
		if(db==null)
		{
			openDB(context);
		}
		//Cursor���α��� �α�����ݿ�����ʵ����һ����ݼ�
        Cursor cursor = db.rawQuery("select count(*) from activity_info", null);
		if(cursor.moveToLast()){
		return cursor.getInt(0);
		}
		return 0;
		
	}
	
	/**
	 * ����ݿ��У����������µĻ��Ϣ
	 * @param storecode
	 * @return
	 */
	public static List<ActivityInfo> findActivityInfoFromDB(Context context,Boolean bisNew)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery(
					"select * from activity_info where isNew=?",
					new String[] { "Yes" });//���������µĻ��Ϣ

			List<ActivityInfo> activitylist = new ArrayList<ActivityInfo>();

			try {
				while (cursor.moveToNext()) {
					ActivityInfo activityInfo=getActivityInfoFromCursor(cursor);
					activitylist.add(activityInfo);

				}
				cursor.close();
				return activitylist;
			} catch (Exception e) {
				e.printStackTrace();
			}

			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * ����ݿ��У�����ŵ��Ż�ȡ��Ӧ�Ļ��Ϣ
	 * @param storecode
	 * @return
	 */
	public static ActivityInfo findActivityInfoFromDB(Context context,Integer uid)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from activity_info where _id=?",
					new String[] { uid.toString() });
			
			if (cursor.moveToFirst()) {
				ActivityInfo activityInfo=getActivityInfoFromCursor(cursor);
				return activityInfo;
			}

			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * ����ݿ��У�����ŵ��Ż�ȡ��Ӧ�Ļ��Ϣ�б�
	 * @param storecode
	 * @return
	 */
	public static List<ActivityInfo> findActivityInfoFromDB(Context context,StoreInfo storeInfo)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from activity_info where storecode=?", new String[] { storeInfo.storecode });

			List<ActivityInfo> activityInfolist=new ArrayList<ActivityInfo>();
			
			while(cursor.moveToNext())
			{
				ActivityInfo activityInfo=getActivityInfoFromCursor(cursor);
                activityInfolist.add(activityInfo);
                
			}
			
			cursor.close();
			return activityInfolist;
			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
     * ����ݿ⣬�������л��Ϣ
     * @return
     */
    public static List<ActivityInfo> loadActivityInfoFromDB(Context context)
 {
		Log.i("Tag", "����ݿ⣬�������л��Ϣ");
		activitylist.clear();
		int total = DBManager.getActivityInfoCount(context);
		Log.i("Tag", "total:" + total);
		for (int uid = 1; uid < total; uid++) {
			ActivityInfo store = DBManager.findActivityInfoFromDB(context, uid);
			activitylist.add(store);
		}
		return activitylist;

	}
	/**
	 * ��ݻ���,���������Ϣ,�����Ϣ�Ѿ����ڣ����滻ԭ��������
	 * @param activityInfo
	 * @return
	 */
	public static int  saveActivityInfotoDB(ActivityInfo activityInfo)
	{
		Log.d("Tag", "����:"+activityInfo.id);
	
		ContentValues cv = new ContentValues();
		cv.put("id", activityInfo.id);
		cv.put("storecode", activityInfo.code);
		cv.put("name", activityInfo.name);
		cv.put("type", activityInfo.type);
		cv.put("starttime", activityInfo.startTime);
		cv.put("endtime", activityInfo.endTime);
		cv.put("level", activityInfo.level);
		cv.put("content", activityInfo.content);
		cv.put("state", activityInfo.state);
		cv.put("imagepath", activityInfo.imagePath);
		cv.put("remark", activityInfo.remark);
		//��������
		cv.put("storename", activityInfo.storeName);//�ŵ����
		cv.put("storetype", activityInfo.storeType);//�ŵ�����
		cv.put("display", activityInfo.display);
		cv.put("displaycount", activityInfo.displaycount);
		cv.put("promostime", activityInfo.promostime);
		cv.put("promoetime", activityInfo.promoetime);
		cv.put("promocount", activityInfo.promocount);
		cv.put("promostate", activityInfo.promostate);
		cv.put("realdisplay", activityInfo.realdisplay);
		cv.put("isphoto", activityInfo.isphoto);
		cv.put("isNew", "NO");
		
		int update = db.update("activity_info", cv, "storecode=? and id=?",
				new String[] { activityInfo.code,activityInfo.id});
		Log.d("saveActivityInfotoDB(activityInfo)", "����:"+activityInfo.id+"...."+activityInfo.name+"........"+activityInfo.code+"......."+activityInfo.storeName);
		activityInfo.isNew = "No";
		/*����ݴ������activitycheck_info��*/
		ActivityCheckInfo activityCheckInfo=transActivityInfoIntoActivityCheckInfo(activityInfo);
		if(activityCheckInfo != null){
			saveNewActivityCheckInfotoDB(activityCheckInfo);
			
		}
		
		
		return update;
	}
	
	public static ActivityInfo getActivityInfoFromCursor(Cursor cursor)
	{
		String id = cursor.getString(cursor.getColumnIndex("id"));//�id
		String storecode = cursor.getString(cursor.getColumnIndex("storecode"));
		String name = cursor.getString(cursor
				.getColumnIndex("name"));
		
		String type = cursor.getString(cursor.getColumnIndex("type"));

		String starttime = cursor.getString(cursor.getColumnIndex("starttime"));
		String endtime = cursor.getString(cursor
				.getColumnIndex("endtime"));
		String level = cursor.getString(cursor
				.getColumnIndex("level"));
		String content = cursor.getString(cursor.getColumnIndex("content"));
		String state = cursor.getString(cursor.getColumnIndex("state"));
		String imagepath = cursor.getString(cursor
				.getColumnIndex("imagepath"));
		String remark = cursor.getString(cursor.getColumnIndex("remark"));
		//������Ŀ
		String storename = cursor.getString(cursor
				.getColumnIndex("storename"));
		String storetype = cursor.getString(cursor
				.getColumnIndex("storetype"));
		String display = cursor.getString(cursor
				.getColumnIndex("display"));
		String displaycount = cursor.getString(cursor
				.getColumnIndex("displaycount"));
		String promostime = cursor.getString(cursor
				.getColumnIndex("promostime"));
		String promoetime = cursor.getString(cursor
				.getColumnIndex("promoetime"));
		String promocount = cursor.getString(cursor
				.getColumnIndex("promocount"));
		String promostate = cursor.getString(cursor
				.getColumnIndex("promostate"));
		String realdisplay = cursor.getString(cursor
				.getColumnIndex("realdisplay"));
		String isPhoto = cursor.getString(cursor
				.getColumnIndex("isphoto"));
		String isNew = cursor.getString(cursor
				.getColumnIndex("isNew"));

		ActivityInfo activityInfo=new ActivityInfo();
		activityInfo.name=name;
		activityInfo.code=storecode;//�ŵ���
		activityInfo.level=level;
		activityInfo.state=state;
		activityInfo.content=content;
		activityInfo.startTime=starttime;
		activityInfo.endTime=endtime;
		activityInfo.type=type;
		activityInfo.remark=remark;
		activityInfo.imagePath=imagepath;
		activityInfo.id=id;//�id
		//������Ŀ
		activityInfo.storeName=storename;
		activityInfo.storeType=storetype;
		activityInfo.display = display;
		activityInfo.displaycount = displaycount;
		activityInfo.promostime = promostime;
		activityInfo.promoetime = promoetime;
		activityInfo.promocount = promocount;
		activityInfo.promostate = promostate;
		activityInfo.realdisplay = realdisplay;
		activityInfo.isphoto=isPhoto;
		activityInfo.isNew = isNew;
		
		return activityInfo;
	}
	
	
	/********************************************************************************
     *                                  ����ָ����ݴ���
     ********************************************************************************/
    /**
     * �ѳ���ָ����ݲ�����ݿ�
     */
    public static void InsertExguideDB(List<ExguideInfo> list)
    {
        for (ExguideInfo exguideInfo : list) {

            ContentValues cv = new ContentValues();

            cv.put("id", exguideInfo.id);
            cv.put("No", exguideInfo.No);
            cv.put("name", exguideInfo.name);
            cv.put("imgpath", exguideInfo.imgpath);
            cv.put("remark", exguideInfo.remark);
            cv.put("type", exguideInfo.type);

            db.insert("exguide_info", null, cv);

        }
    }
    /**
     * ������������г���ָ�����
     * @param context
     */
    public static void loadExguideInfo(Context context,String strUrl)
    {
        try {
            if (db == null) {
                openDB(context);
            }
            String response=HttpUtil.HTTPGet(strUrl);
             List<ExguideInfo> exguideInfos = ExguideInfoSAXService.readXml(response);//����
            InsertExguideDB(exguideInfos);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * ��ȡ����ָ����Ϣ�ܼ�¼��
     * @param context
     * @return
     */
    public static int getExguideInfoCount(Context context)
    {
        if(db==null)
        {
            openDB(context);
        }
        //Cursor���α��� �α�����ݿ�����ʵ����һ����ݼ�
        Cursor cursor = db.rawQuery("select count(*) from exguide_info", null);
        if(cursor.moveToLast()){
        return cursor.getInt(0);
        }
        return 0;
        
    }
    
    /**
     * @param storecode
     * @return
     */
    public static List<ExguideInfo> findExguideInfoFromDB(Context context)
    {
        try {
            if (db == null) {
                openDB(context);
            }
//            Cursor cursor = db.rawQuery("select * from exguide_info", new String[] { "" });
            Cursor cursor = db.query("exguide_info", null, null, null, null, null, null);
            
            cursor.moveToFirst();
            List<ExguideInfo> exguideInfos = new ArrayList<ExguideInfo>();

            try {
                do {
                    ExguideInfo exguideInfo=getExguideInfoFromCursor(cursor);
                    exguideInfos.add(exguideInfo);
                }while (cursor.moveToNext()); 
                cursor.close();
                return exguideInfos;
            } catch (Exception e) {
                e.printStackTrace();
            }

            
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;

    }
    /**
     * ����ݿ��У�����ŵ��Ż�ȡ��Ӧ�ĳ�����Ϣ
     * @param storecode
     * @return
     */
    public static ExguideInfo findExguideInfoFromDB(Context context,Integer uid)
    {
        try {
            if (db == null) {
                openDB(context);
            }
            Cursor cursor = db.rawQuery("select * from exguide_info where _id=?",
                    new String[] { uid.toString() });
            
            if (cursor.moveToFirst()) {
                ExguideInfo exguideInfo=getExguideInfoFromCursor(cursor);
                return exguideInfo;
            }

            
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;

    }
    /**
     * ����ݿ��У�����ŵ��Ż�ȡ��Ӧ�ĳ���ָ����Ϣ�б�
     * @param storecode
     * @return
     */
    public static ExguideInfo findExguideInfoFromDB(Context context,StoreInfo storeInfo)
    {
        try {
            if (db == null) {
                openDB(context);
            }
            Cursor cursor = db.rawQuery("select * from exguide_info where type=?", new String[] { storeInfo.type });

            ExguideInfo exguideInfo = null ;
            while(cursor.moveToNext())
            {
                exguideInfo=getExguideInfoFromCursor(cursor);
                Log.d("db-info", "�ŵ����� "+ storeInfo.type +"�ĳ���ָ�������");
            }
            
            cursor.close();
            return exguideInfo;
            
        } catch (Exception e) {

            e.printStackTrace();
        }

        return null;

    }
    /**
     * ����ݿ⣬�������г���ָ����Ϣ
     * @return
     */
    public static List<ExguideInfo> loadExguideInfoFromDB(Context context)
 {
        Log.i("Tag", "����ݿ⣬�������г���ָ��");
        exguidelist.clear();
        int total = DBManager.getExguideInfoCount(context);
        Log.i("Tag", "total:" + total);
        for (int uid = 1; uid < total; uid++) {
            ExguideInfo store = DBManager.findExguideInfoFromDB(context, uid);
            exguidelist.add(store);
        }
        return exguidelist;

    }
    /**
     * ��ݻ���,���������Ϣ,�����Ϣ�Ѿ����ڣ����滻ԭ��������
     * @param exguideInfo
     * @return
     */
    public static int  saveExguideInfotoDB(ExguideInfo exguideInfo)
    {
        Log.d("Tag", "����:"+exguideInfo.id);
    
        ContentValues cv = new ContentValues();

        cv.put("id", exguideInfo.id);
        cv.put("No", exguideInfo.No);
        cv.put("name", exguideInfo.name);
        cv.put("imgpath", exguideInfo.imgpath);
        cv.put("remark", exguideInfo.remark);
        cv.put("type", exguideInfo.type);
        
        int update = db.update("activity_info", cv, "storecode=? and id=?",
                new String[] { exguideInfo.No,exguideInfo.id});
        
        return update;
    }
    
    
    public static ExguideInfo getExguideInfoFromCursor(Cursor cursor)
    {
        String id = cursor.getString(cursor.getColumnIndex("id"));
        String No = cursor.getString(cursor.getColumnIndex("No"));
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String type = cursor.getString(cursor.getColumnIndex("type"));
        String remark = cursor.getString(cursor.getColumnIndex("remark"));
        String imgpath = cursor.getString(cursor.getColumnIndex("imgpath"));

        ExguideInfo exguideInfo=new ExguideInfo();
        exguideInfo.id=id;
        exguideInfo.No=No;
        exguideInfo.name=name;
        exguideInfo.remark=remark;
        exguideInfo.imgpath=imgpath;
        exguideInfo.type=type;
        
        return exguideInfo;
    }
    

	/**************************************************************************
	 *               ����������ݴ���  By ADao12
	 *               ���ء����桢��ȡ��չʾ�������¼�¼���ϴ�/ͬ��
	 **************************************************************************/
	
	/**
	 * �ѻ��������б���ݲ�����ݿ�,��store_code
	 */
	public static void InsertActivityCheckDB(List<ActivityCheckInfo> list,Context context)
	{
		for (ActivityCheckInfo activityCheckInfo : list) {

			ContentValues cv = new ContentValues();

			cv.put("check_id", activityCheckInfo.check_id);
			cv.put("activity_id", activityCheckInfo.activity_id);
			cv.put("store_code", activityCheckInfo.store_code);
			cv.put("promostate", activityCheckInfo.promostate);
			cv.put("promoproductstate", activityCheckInfo.promoproductstate);
			cv.put("materialstate", activityCheckInfo.materialstate);
			cv.put("realdisplay", activityCheckInfo.realdisplay);
			cv.put("isphoto", activityCheckInfo.isphoto);
			cv.put("level", activityCheckInfo.level);
			cv.put("state", activityCheckInfo.state);
			cv.put("remark", activityCheckInfo.remark);
			cv.put("imagePath", activityCheckInfo.imagePath);
			//Ĭ�������������²��ɸ�ĵĻ����¼
			cv.put("isOld", activityCheckInfo.isOld);

			db.insert("activitycheck_info", null, cv);
		}
		getUserActivityandStoreCharged(context);
	}
	
	/*
	 * ����û�������ŵ�s,������DataApplication.Saar.storeSimpleMsg��
	 * @return List<String>
	 */
	public static List<StoreSimpleMsg> getStoresOfUserCharged(){
		List<StoreSimpleMsg> SSMList = new ArrayList<StoreSimpleMsg>();
		
		StoreAndActivityRecord SAAR;
		Cursor cursor = db.query(true, "activity_info", new String[]{"storecode","storeName"},
				"storecode=?", null, null, null, null, null);
		while(cursor.moveToNext()){
			StoreSimpleMsg SSM = null;
			SSM.store_code = cursor.getString(cursor.getColumnIndex("storecode"));
			SSM.store_name = cursor.getString(cursor.getColumnIndex("storeName"));
			if(SSM != null){
				SAAR = new StoreAndActivityRecord();
				SAAR.storeMsg.store_code = SSM.store_code;
				SAAR.storeMsg.store_name = SSM.store_name;
				DataApplication.Saar.add(SAAR);
				
				SSMList.add(SSM);
			}
			
		}
		cursor.close();
		
		return SSMList;
	}
	
	/*
	 * ����û�������ŵ��ţ���õ��û�����Ļs
	 * DataApplication.Sarr will change
	 */
	public static List<ActivitySimpleMsg> getActivitysOfStoreOfUserCharged(String store_code){
		List<ActivitySimpleMsg> activitySimpleMsgs = new ArrayList<StoreAndActivityRecord.ActivitySimpleMsg>();
		
		Cursor cursor = db.query(true, "activity_info", new String[]{"id","name"}, "storecode=?", new String[]{store_code}, null, null, null, null);//TODO check
		Log.v("cursor1", cursor.toString());
		ActivitySimpleMsg activitySimpleMsg;
		while(cursor.moveToNext()){
			activitySimpleMsg = new ActivitySimpleMsg();
			Log.v("cursor2", cursor.toString());
			Log.v("cursor31", cursor.getString(cursor.getColumnIndex("id")));
			Log.v("cursor32", cursor.getString(cursor.getColumnIndex("name")));
			
			activitySimpleMsg.activity_id = cursor.getString(cursor.getColumnIndex("id")).toString();
			Log.v("cursor333", cursor.toString());
			activitySimpleMsg.activity_name = cursor.getString(cursor.getColumnIndex("name")).toString();
			if(activitySimpleMsg != null){
				activitySimpleMsgs.add(activitySimpleMsg);
			}
		}
		Log.v("cursor4", cursor.toString());
		cursor.close();
		return activitySimpleMsgs;
		
	}
	
	/*
	 * ����û�������ŵ�s�ͻs
	 */
	public static void getStoresAndActivitysOfUserCharged(Context context){
		if(db == null){
			openDB(context);
		}
		getStoresOfUserCharged();//����û�������ŵ�
		
		for (int i = 0; i < DataApplication.Saar.size(); i++) {
			String storecode = DataApplication.Saar.get(i).storeMsg.store_code;
			DataApplication.Saar.get(i).activityMsg = getActivitysOfStoreOfUserCharged(storecode);
		}
	}
	
	/*
	 * ����ŵ��š��ID,��û����¼�б�
	 */
	public static List<ActivityCheckInfo> getActivityCheckRecordsOfActivity(Context context, String store_code, String activity_id){
		if(db == null){
			openDB(context);
		}
		if (DataApplication.activityCheckInfoList != null) {
			DataApplication.activityCheckInfoList.clear();
		}
		List<ActivityCheckInfo> activityCheckInfos = new ArrayList<ActivityCheckInfo>();
		Cursor cursor = db.rawQuery("select * from activitycheck_info where store_code=? and activity_id=?",
				new String[]{store_code, activity_id});
		ActivityCheckInfo activityCheckInfo;
		while(cursor.moveToNext()){
			activityCheckInfo = getActivityCheckInfoFromCursor(cursor);
			
			DataApplication.activityCheckInfoList.add(activityCheckInfo);
			
			activityCheckInfos.add(activityCheckInfo);
		}
		cursor.close();
		
		return activityCheckInfos;
	}
	
	/*
	 * ����û������ĳ�ŵ��ĳЩ�
	 * DataApplication.Sarr will change
	 */
	public static void getActivityCheckRecordsOfActivityOfUserCharged(Context context, String store_code, String activity_code){
		if(db == null){
			openDB(context);
		}
		if(DataApplication.Saar != null){
			DataApplication.Saar.clear();
		}
		List<StoreSimpleMsg> tempSList = getStoresOfUserCharged();
		StoreAndActivityRecord SAAR;
		for (StoreSimpleMsg storecode : tempSList) {
			SAAR = new StoreAndActivityRecord();
			SAAR.storeMsg.store_code = storecode.store_code;
			SAAR.storeMsg.store_name = storecode.store_name;
			Cursor cursor = db.query(true, "activity_info", new String[]{"id","name"},"storecode=?", new String[]{SAAR.storeMsg.store_code}, null, null, null, null);
			while(cursor.moveToNext()){
				ActivitySimpleMsg ASM = null;
				ASM.activity_id = cursor.getString(cursor.getColumnIndex("id"));
				ASM.activity_name = cursor.getString(cursor.getColumnIndex("name"));
				SAAR.activityMsg.add(ASM);
			}
			DataApplication.Saar.add(SAAR);
		}
	}
	
	/*
	 * ����û������ĳ�ŵ��ĳЩ�
	 */
	public static void getUserActivityandStoreCharged(Context context){
		if(db == null){
			openDB(context);
		}
		Cursor cursor = db.rawQuery("select DISTINCT store_code from activitycheck_info where store_code=?",null);
		Boolean isExist = false;
		StoreAndActivityRecord SAAR = new StoreAndActivityRecord();
		while(cursor.moveToNext()){
			for(int i=0; i<DataApplication.Saar.size(); i++){
				SAAR.storeMsg.store_code = cursor.getString(cursor.getColumnIndex("store_code"));
				if(DataApplication.Saar.get(i).storeMsg.store_code.equals(SAAR.storeMsg.store_code)){
					isExist = true;
				}
			}
			if(!isExist){
				DataApplication.Saar.add(SAAR);
			}else{
				isExist = false;
			}
		}
		
		isExist = false;
		ActivitySimpleMsg ASM = null;
		for(int i=0; i<DataApplication.Saar.size(); i++){
			Cursor cursor2 = db.rawQuery("select * from activitycheck_info where store=?",new String[]{DataApplication.Saar.get(i).storeMsg.store_code});
			while(cursor2.moveToNext()){
				for(int j=0; j<DataApplication.Saar.get(i).activityMsg.size(); j++){
					ASM.activity_id = cursor.getString(cursor.getColumnIndex("activity_id"));
					ASM.activity_name = cursor.getString(cursor.getColumnIndex("activity_name"));
					if(DataApplication.Saar.get(i).activityMsg.get(j).activity_id.equals(ASM.activity_id)){
						isExist = true;
					}
				}
				if(!isExist){
					DataApplication.Saar.get(i).activityMsg.add(ASM);
				}else{
					isExist = false;
				}
			}
		}
	}
	
	/**
	 * �ѣ��ɵģ������¼��ݲ�����ݿ�
	 */
	public static void InsertActivityCheckDB(ActivityCheckInfo activityCheckInfo)
	{
			ContentValues cv = new ContentValues();

			cv.put("check_id", activityCheckInfo.check_id);
			cv.put("activity_id", activityCheckInfo.activity_id);
			cv.put("store_code", activityCheckInfo.store_code);
			cv.put("promostate", activityCheckInfo.promostate);
			cv.put("promoproductstate", activityCheckInfo.promoproductstate);
			cv.put("materialstate", activityCheckInfo.materialstate);
			cv.put("realdisplay", activityCheckInfo.realdisplay);
			cv.put("isphoto", activityCheckInfo.isphoto);
			cv.put("level", activityCheckInfo.level);
			cv.put("state", activityCheckInfo.state);
			cv.put("remark", activityCheckInfo.remark);
			cv.put("imagePath", activityCheckInfo.imagePath);
			//Ĭ��Ϊ���µ���ݣ�����鵽activityCheckInfo.isOld.equals("No"),�������ݿɽ��С�����(update)������
			if(activityCheckInfo.isOld.equals("Yes") || activityCheckInfo.isOld.equals("yes")){
				cv.put("isOld", "Yes");
			}else{
				cv.put("isOld", activityCheckInfo.isOld);
			}
			

			db.insert("activitycheck_info", null, cv);
	}
	
	
	
	/*
	 * �����ص�������isNew.equalsWith("Yes")��ActivityInfoת��ActivityCheckInfo��������תΪ���ɸ�ĵĻ����¼
	 */
	public static ActivityCheckInfo transActivityInfoIntoActivityCheckInfo(ActivityInfo activityInfo){
		
		if(activityInfo != null){
			if(activityInfo.isNew.endsWith("No") || activityInfo.isNew.endsWith("no")){
				//�����»��ת��
				ActivityCheckInfo activityCheckInfo = new ActivityCheckInfo();
				activityCheckInfo.activity_id = activityInfo.id;
				activityCheckInfo.store_code = activityInfo.code;
				activityCheckInfo.level = activityInfo.level;
				activityCheckInfo.state = activityInfo.state;
				activityCheckInfo.remark = activityInfo.remark;
				activityCheckInfo.imagePath = activityInfo.imagePath;
				activityCheckInfo.materialstate = activityInfo.materialstate;
				activityCheckInfo.promoproductstate = activityInfo.promoproductstate;
				activityCheckInfo.promostate = activityInfo.promostate;
				activityCheckInfo.realdisplay = activityInfo.realdisplay;
				activityCheckInfo.isphoto = activityInfo.isphoto;
				activityCheckInfo.isOld = "Yes";          //ָʾ�����¼���ɸ��
				Log.d("transActivityInfoIntoActivityCheckInfo", "����:"+activityCheckInfo.activity_id+"...."+activityCheckInfo.check_id+"........"+activityCheckInfo.store_code);
				return activityCheckInfo;
			  }
		}
		return null;
		
		
	}
	
	/*
     * ��ȡ�����¼��
     * @param context
     * @return
     */
    public static int getActivityCheckInfoCount(Context context){//TODO might not need to do this method
    	if(db == null){
    		openDB(context);
    	}
    	
    	Cursor cursor = db.rawQuery("select count(*) from activitycheck_info", null);
    	if(cursor.moveToLast()){
    		return cursor.getInt(0);
    	}
    	return 0;
    }
	
	/**
	 * ������������л����������,��������ݱ�activitycheck_info����
	 * @param context
	 */
	public static void loadActivityCheckInfo(Context context,String strUrl)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			String response=HttpUtil.HTTPGet(strUrl);//������������л����������
			 List<ActivityCheckInfo> activityCheckInfos = ActivityCheckInfoSAXService.readXml(response);//����
			InsertActivityCheckDB(activityCheckInfos,context);//������ݿ��activitycheck_info����
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	//���������봢��
	
	//TODO need to check again by ADao12
	/*
	 * ͨ���α�cursor�ı�����һ������¼���������ݱ���һ�е���ݣ�����ActivityCheckInfo
	 * @param cursor
	 * @return ActivityCheckInfo
	 */
	public static ActivityCheckInfo getActivityCheckInfoFromCursor(Cursor cursor){
		
		String check_id = cursor.getString(cursor.getColumnIndex("check_id"));//��������id
		String activity_id = cursor.getString(cursor.getColumnIndex("activity_id"));//�id
		String store_code = cursor.getString(cursor.getColumnIndex("store_code"));//�ŵ�id
		String promostate = cursor.getString(cursor.getColumnIndex("promostate"));
		String promoproductstate = cursor.getString(cursor.getColumnIndex("promoproductstate"));
		String materialstate = cursor.getString(cursor.getColumnIndex("materialstate"));
		String level = cursor.getString(cursor.getColumnIndex("level"));
		String state = cursor.getString(cursor.getColumnIndex("state"));
		String realdisplay = cursor.getString(cursor.getColumnIndex("realdisplay"));
		String isphoto = cursor.getString(cursor.getColumnIndex("isphoto"));
		String remark = cursor.getString(cursor.getColumnIndex("remark"));
		String imagePath = cursor.getString(cursor.getColumnIndex("imagePath"));
		String isOld = cursor.getString(cursor.getColumnIndex("isOld"));

		ActivityCheckInfo activityCheckInfo=new ActivityCheckInfo();
		activityCheckInfo.check_id = check_id;
		activityCheckInfo.activity_id = activity_id;
		activityCheckInfo.store_code = store_code;
		activityCheckInfo.level=level;
		activityCheckInfo.state=state;
		activityCheckInfo.remark=remark;
		activityCheckInfo.imagePath=imagePath;
		activityCheckInfo.materialstate = materialstate;
		activityCheckInfo.promoproductstate = promoproductstate;
		activityCheckInfo.promostate = promostate;
		activityCheckInfo.realdisplay = realdisplay;
		activityCheckInfo.isphoto= isphoto;
		activityCheckInfo.isOld = isOld;
		
		return activityCheckInfo;
	}
	
	
	/**
	 * ��ݻ���������,�����������������Ϣ,�����Ϣ���µģ���δͬ���������������Ѿ����ڣ����滻ԭ������Ϣ����
	 * @param activityInfo
	 * @return
	 */
	public static void  saveNewActivityCheckInfotoDB(ActivityCheckInfo activityCheckInfo)//TODO have to change to insert the new activitycheck msg
	{
		
		Cursor cursor = db.rawQuery("select * from activitycheck_info where store_code=? and activity_id=?", 
				new String[]{activityCheckInfo.store_code,activityCheckInfo.activity_id});
		String temp = "0";
		while(cursor.moveToNext()){
			String s = cursor.getString(cursor.getColumnIndex("check_id"));
			if(temp.compareTo(s)<0){
				temp = s;
			}
		}
		if (temp == null) {
			activityCheckInfo.check_id = String.valueOf(0);
		}else{
			int newcheckid = Integer.parseInt(temp);
			newcheckid++;
			activityCheckInfo.check_id = String.valueOf(newcheckid);
		}

		Log.d("Tag", "����������:"+activityCheckInfo.check_id);
		
		ContentValues cv = new ContentValues();
		cv.put("check_id", activityCheckInfo.check_id);
		cv.put("activity_id", activityCheckInfo.activity_id);
		cv.put("store_code", activityCheckInfo.store_code);
		cv.put("materialstate", activityCheckInfo.materialstate);
		cv.put("level", activityCheckInfo.level);
		cv.put("state", activityCheckInfo.state);
		cv.put("remark", activityCheckInfo.remark);
		cv.put("promoproductstate", activityCheckInfo.promoproductstate);
		cv.put("promostate", activityCheckInfo.promostate);
		cv.put("realdisplay", activityCheckInfo.realdisplay);
		cv.put("isphoto", activityCheckInfo.isphoto);
		cv.put("imagePath", activityCheckInfo.imagePath);
		cv.put("isOld", activityCheckInfo.isOld);
		Log.d("db.insert(activitycheck)", "����:"+activityCheckInfo.activity_id);
		db.insert("activitycheck_info", null, cv);
		
	}
	//���˱�����ϣ���ʱֱ�Ӹ��´˱?��
	
	/**
     * ����ݿ⣬�������л��Ϣ
     * @return
     */
    public static List<ActivityCheckInfo> loadActivityCheckInfoFromDB(Context context){//TODO when divoke the method, check and redo it
		Log.i("Tag", "����ݿ⣬�������л��Ϣ");
		List<ActivityCheckInfo> activityChecklist = new ArrayList<ActivityCheckInfo>();
		activityChecklist.clear();
		
		
		int total = DBManager.getActivityCheckInfoCount(context);//TODO that's not what we want to get
		Log.i("Tag", "total:" + total);
		for (int i = 0; i < DataApplication.Saar.size(); i++) {
			for (int j = 0; j < DataApplication.Saar.get(i).activityMsg.size(); j++) {
				ActivityCheckInfo store = DBManager.findActivityCheckInfoFromDB(context, i, j);
				activityChecklist.add(store);
			}
		}
		
		return activityChecklist;

	}
    
    /*
	 * ���һ��ѯ����ļ�¼��Ϣ�б�,//TODO this method have a big problem
	 */
	public static List<ActivityCheckInfo> findCheckedRecordofActivityFromDB(Context context, Integer activity_id){
		try {
			if(db == null){
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from activitycheck_info where activity_id=?",
					new String[] { activity_id.toString() });
			if (cursor.moveToFirst()) {
				ActivityCheckInfo activityCheckInfo=getActivityCheckInfoFromCursor(cursor);
				if(activityCheckInfo.isOld.equals("Yes")){//���ڼ���¼
					Cursor cursor2 = db.rawQuery("select * from activitycheck_info where activity_id=?", 
							new String[]{activityCheckInfo.activity_id});
					
						List<ActivityCheckInfo> activityCheckInfos = new ArrayList<ActivityCheckInfo>();
						while (cursor2.moveToNext()) {
							ActivityCheckInfo activityCheckInfo2=getActivityCheckInfoFromCursor(cursor);
							activityCheckInfos.add(activityCheckInfo2);
						}
						cursor2.close();
						return activityCheckInfos;
					
				}
			}
			cursor.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	//���˶�ȡ��ϣ�չʾActivity��ʵ��
    
    /*
	 * add by ADao12 //TODO gain the activitycheckinfoFromDB
	 * @param context
	 * return list of activityCheckInfo
	 */
	public static List<ActivityCheckInfo> findActivityCheckInfoFromDB(Context context){
		
		List<ActivityCheckInfo> activityCheckInfoList = new ArrayList<ActivityCheckInfo>();
		
		if(activityCheckInfoList.size() > 0){
			activityCheckInfoList.clear();
			//TODO: need to redo
		}
		else{
			int total = DBManager.getActivityCheckInfoCount(context);
			
			for(int uid=1;uid<total;uid++) {
				ActivityCheckInfo activityCheckInfo = DBManager.findActivityCheckInfo(context, uid);
				activityCheckInfoList.add(activityCheckInfo);
				
				/*String activity_id = activityCheckInfo.activity_id;
				String promostate = activityCheckInfo.promostate;
				String promoproductstate = activityCheckInfo.promoproductstate;
				String materialstate = activityCheckInfo.materialstate;
				String realdisplay = activityCheckInfo.realdisplay;
				String isphoto = activityCheckInfo.isphoto;
				String level = activityCheckInfo.level;
				String state = activityCheckInfo.state;
				String remark = activityCheckInfo.remark;
				String imagePath = activityCheckInfo.imagePath;*/
				//����ݿɼ���ApplicationData����

			}
		}
		return activityCheckInfoList;
	}
	
	/*
	 * ���һ����¼��Ϣ��һ�����   2013.1.23
	 */
	public static ActivityCheckInfo findActivityCheckInfo(Context context, Integer uid){
		ActivityCheckInfo activityCheckInfo = new ActivityCheckInfo();
		try {
			if(db == null){
				openDB(context);
			}
			Cursor cursor = db.rawQuery("selcet * activitycheck_info where _id=?",
					new String[]{uid.toString()});
			if (cursor.moveToFirst()) {
				String activity_id = cursor.getString(cursor.getColumnIndex("activity_id"));
				String promostate = cursor.getString(cursor.getColumnIndex("promostate"));
				String promoproductstate = cursor.getString(cursor.getColumnIndex("promoproductstate"));
				String materialstate = cursor.getString(cursor.getColumnIndex("materialstate"));
				String realdisplay = cursor.getString(cursor.getColumnIndex("realdisplay"));
				String isphoto = cursor.getString(cursor.getColumnIndex("isphoto"));
				String level = cursor.getString(cursor.getColumnIndex("level"));
				String state = cursor.getString(cursor.getColumnIndex("state"));
				String remark = cursor.getString(cursor.getColumnIndex("remark"));
				String imagePath = cursor.getString(cursor.getColumnIndex("imagePath"));
				String isOld = cursor.getString(cursor.getColumnIndex("isOld"));
				
				activityCheckInfo.activity_id = activity_id;
				activityCheckInfo.promostate = promostate;
				activityCheckInfo.promoproductstate = promoproductstate;
				activityCheckInfo.materialstate = materialstate;
				activityCheckInfo.realdisplay = realdisplay;
				activityCheckInfo.isphoto = isphoto;
				activityCheckInfo.level = level;
				activityCheckInfo.state = state;
				activityCheckInfo.remark = remark;
				activityCheckInfo.imagePath = imagePath;
				activityCheckInfo.isOld = isOld;
				
				cursor.close();
				return activityCheckInfo;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * ����ݿ��У����������µĻ���������Ϣ��Ϣ
	 * ��bisOld=trueʱ��Ѱ����ǰ�ļ���¼����֮��
	 * @param context bisOld
	 * @return List<ActivityCheckInfo> when can find or null
	 */
	public static List<ActivityCheckInfo> findActivityCheckInfoFromDB(Context context,Boolean bisOld)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor;
			if(bisOld){
				cursor = db.rawQuery("select * from activitycheck_info where isOld=?",new String[] { "Yes" });
			}else{
				cursor = db.rawQuery("select * from activitycheck_info where isOld=?",new String[] { "No" });
			}
			
			List<ActivityCheckInfo> activitychecklist = new ArrayList<ActivityCheckInfo>();

			try {
				while (cursor != null && cursor.moveToNext()) {
					ActivityCheckInfo activityCheckInfo=getActivityCheckInfoFromCursor(cursor);
					activitychecklist.add(activityCheckInfo);

				}
				cursor.close();
				return activitychecklist;
			} catch (Exception e) {
				e.printStackTrace();
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}

	
	/**
	 * ����ݿ��У���ݻID���ŵ�ID��ȡ��Ӧ�Ļ�����Ϣ
	 * @param 
	 * @return
	 */
	public static ActivityCheckInfo findActivityCheckInfoFromDB(Context context,Integer activityID, Integer storeID)
	{
		try {
			if (db == null) {
				openDB(context);
			}//TODO to check this 
			Cursor cursor = db.rawQuery("select * from activitycheck_info where activity_id=? and store_code=?",
					new String[] { activityID.toString(),storeID.toString()});
			
			if (cursor.moveToFirst()) {
				ActivityCheckInfo activityCheckInfo=getActivityCheckInfoFromCursor(cursor);
				cursor.close();
				return activityCheckInfo;
			}
			cursor.close();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * ����ݿ��У���ݻID���ŵ�ID��ȡ��Ӧ�Ļ�����Ϣ�б�
	 * @param storecode
	 * @return
	 */
	public static List<ActivityCheckInfo> findActivityCheckInfosFromDB(Context context,Integer activityID, Integer storeID)
	{//this method is wrong
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from activitycheck_info where activity_id=? and store_code=?",
					new String[] { activityID.toString(),storeID.toString()});

			List<ActivityCheckInfo> activityCheckInfolist=new ArrayList<ActivityCheckInfo>();
			
			while(cursor.moveToNext())
			{
				ActivityCheckInfo activityCheckInfo=getActivityCheckInfoFromCursor(cursor);
                activityCheckInfolist.add(activityCheckInfo);
			}
			
			cursor.close();
			return activityCheckInfolist;
			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	
    
    
	
	/*************************************************************************************************************
	 * ��һ������public static void loadAllInfoByStoreCode(Context context,String userName,String type)�������
	 *************************************************************************************************************/

	
	/**
	 * �������ϼ��������ŵ��������Ϣ�����������ͬ������ͳ�ơ���Ʒ��̬  ����ָ��
	 */
	public static void loadAllInfoByStoreCode(Context context,String userName,String type)
	{

		//List<StoreInfo> storelist=loadStoreInfoFromDB(context);
		String Url = IConst.ServerIP+"data/user!getStorePlan.action?"
		+ "username="
		+ "&type="
		+ type;
		
		Log.i("SQL", "���ػ  URL:"+Url);
		
		//1.�
		if("h".equals(type))
		{
			loadActivityInfo(context,Url);
		}

		
		//2.��ͬ
		else if("c".equals(type))
		{
			loadContractInfo(context,Url);
		}
		//3.�����¼
		else{
			//loadActivityCheckInfo(context, Url);
		}
//	      //���³���ָ��  added by xieyan  2013 01 30
//        if("".equals(type)) {
//            String URL = IConst.ServerIP+"data/exhibt.action";
//            loadExguideInfo(context, URL);
//        }


	}
	

	
	
	/********************************************************************************
	 *                                  3.��ͬ��ݴ���
	 ********************************************************************************/
	/**
	 * ������������к�ͬ���
	 * @param context
	 */
	public static void loadContractInfo(Context context,String strUrl)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			String response=HttpUtil.HTTPGet(strUrl);
			 List<ContractInfo> contractInfos = ContractInfoSAXService.readXml(response);
			 InsertContractDB(contractInfos);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * ��ȡ��ͬ�ܼ�¼��
	 * @param context
	 * @return
	 */
	public static int getContractInfoCount(Context context)
	{
		if(db==null)
		{
			openDB(context);
		}
		//Cursor���α��� �α�����ݿ�����ʵ����һ����ݼ�
        Cursor cursor = db.rawQuery("select count(*) from contract_info", null);
		if(cursor.moveToLast()){
		return cursor.getInt(0);
		}
		return 0;
		
	}
	
	/**
	 * �Ѻ�ͬ��ݲ�����ݿ�
	 */
	public static void InsertContractDB(List<ContractInfo> list)
	{
		for (ContractInfo contractInfo : list) {

			ContentValues cv = new ContentValues();
			cv.put("name", contractInfo.name);
			cv.put("id", contractInfo.id);
			cv.put("storename", contractInfo.storeName);//�ŵ����
			cv.put("storecode", contractInfo.storecode);
			cv.put("type", contractInfo.type);
			cv.put("starttime", contractInfo.startTime);
			cv.put("endtime", contractInfo.endTime);
			cv.put("isphoto", contractInfo.isphoto);
			cv.put("content", contractInfo.content);
			if(contractInfo.state==null||(contractInfo.state!=null&&contractInfo.state.length()==0))
			{
				contractInfo.state="δִ��";
			}
			cv.put("state", contractInfo.state);
			cv.put("imagepath", contractInfo.imagePath);
			cv.put("remark", contractInfo.remark);
			cv.put("isNew", "Yes");
			db.insert("contract_info", null, cv);
		}
	}
	/**
	 * ������ݿ��еĺ�ͬ
	 */
	public static void saveContractDB(ContractInfo contractInfo)
	{
		ContentValues cv = new ContentValues();
		cv.put("name", contractInfo.name);
		cv.put("id", contractInfo.id);
		cv.put("storename", contractInfo.storeName);//�ŵ����
		cv.put("storecode", contractInfo.storecode);
		cv.put("type", contractInfo.type);
		cv.put("starttime", contractInfo.startTime);
		cv.put("endtime", contractInfo.endTime);
		cv.put("isphoto", contractInfo.isphoto);
		cv.put("content", contractInfo.content);
		if(contractInfo.state==null||(contractInfo.state!=null&&contractInfo.state.length()==0))
		{
			contractInfo.state="δִ��";
		}
		cv.put("state", contractInfo.state);
		cv.put("imagepath", contractInfo.imagePath);
		cv.put("remark", contractInfo.remark);
		if(!contractInfo.state.equals("δִ��")) {
		    cv.put("isNew", "No");
		}else {
		    cv.put("isNew", "Yes");
		}
		
		try {
			int update = db.update("contract_info", cv, "id=?",
					new String[] {contractInfo.id});
		} catch (Exception e) {
			Log.e("info", " �����ͬ�����ˡ�����  " + contractInfo.name );
			e.printStackTrace();
		}
	}
	/**
	 * ����ݿ��У����������µĺ�ͬ��Ϣ
	 * @param storecode
	 * @return
	 */
	public static List<ContractInfo> findContractInfoFromDB(Context context,Boolean bisNew)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery(
					"select * from contract_info where isNew=?",
					new String[] { "Yes" });
			List<ContractInfo> contractlist = new ArrayList<ContractInfo>();

			try {
				while (cursor != null && cursor.moveToNext()) {

                    ContractInfo contractInfo=getContractInfoFromCursor(cursor);
					contractlist.add(contractInfo);

				}
				cursor.close();
				return contractlist;
			} catch (Exception e) {
				// TODO: handle exception
			}

			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * ����ŵ��ţ���ȡ���ŵ����еĺ�ͬ
	 */
	public static List<ContractInfo> findContractInfoFromDB(Context context,StoreInfo storeInfo)
	{
		try {
			if (db == null) {
				openDB(context);
			}
			Cursor cursor = db.rawQuery("select * from contract_info where storecode=?",
					new String[] { storeInfo.storecode });

			List<ContractInfo> contractInfolist=new ArrayList<ContractInfo>();
			
			while(cursor.moveToNext())
			{
				ContractInfo contractInfo=getContractInfoFromCursor(cursor);
				contractInfolist.add(contractInfo);
                
			}
			
			cursor.close();
			return contractInfolist;


			
		} catch (Exception e) {

			e.printStackTrace();
		}

		return null;

	}
	/**
	 * 
	 * @param cursor
	 * @return
	 */
	public static ContractInfo getContractInfoFromCursor(Cursor cursor)
	{
		String id = cursor.getString(cursor.getColumnIndex("id"));// �id
		String storecode = cursor.getString(cursor
				.getColumnIndex("storecode"));
		String name = cursor.getString(cursor
				.getColumnIndex("name"));
		String storename = cursor.getString(cursor
				.getColumnIndex("storename"));

		String type = cursor.getString(cursor
				.getColumnIndex("type"));

		String starttime = cursor.getString(cursor
				.getColumnIndex("starttime"));
		String endtime = cursor.getString(cursor
				.getColumnIndex("endtime"));
		String content = cursor.getString(cursor
				.getColumnIndex("content"));
		String state = cursor.getString(cursor
				.getColumnIndex("state"));
		String imagepath = cursor.getString(cursor
				.getColumnIndex("imagepath"));
		String remark = cursor.getString(cursor
				.getColumnIndex("remark"));

		String isPhoto = cursor.getString(cursor
				.getColumnIndex("isphoto"));
		String isNew = cursor.getString(cursor
				.getColumnIndex("isNew"));

		//
		ContractInfo contractInfo = new ContractInfo();
		contractInfo.name = name;
		contractInfo.storecode = storecode;// �ŵ���
		contractInfo.state = state;
		contractInfo.content = content;
		contractInfo.startTime = starttime;
		contractInfo.endTime = endtime;
		contractInfo.type = type;
		contractInfo.remark = remark;
		contractInfo.imagePath = imagepath;
		contractInfo.id = id;// ��ͬid
		contractInfo.storeName=storename;
		contractInfo.isphoto=isPhoto;
		contractInfo.isNew = isNew;
		
		return contractInfo;
	}
	
	/*******************************************************************************
	 *                 4. ������ݸ��£�ִ������� by ADao12
	 *******************************************************************************/
	
	
}
